package com.source.hotelbath.weight.radiogp;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.source.hotelbath.R;


/**
 * Created by kinglong on 2016/2/19.
 */
public class RadioItem extends LinearLayout {

    private int mIvIconBkg;
    private float mIcWidth;
    private float mIcHeight;

    private String mTitle;
    private float mTitleSize;
    private int mTitleColorId;

    private boolean mLableVisiable;
    private int mLabelBkgId;
    private int mLabelColorId;
    private float mLabelSize;
    private int mLabelTopMargin;
    private int mLabelLeftMargin;
    private float mLabelHeight;

    private TypedArray mTypedArray;

    private CheckedTextView mCtvIcon;
    private CheckedTextView mCtvTitle;
    private TextView mTvLabel;

    public RadioItem(Context context) {
        super(context);
    }

    public RadioItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.radio_item,this);
        mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.radio_item);
        mCtvIcon = (CheckedTextView) findViewById(R.id.ctv_icon);
        mCtvTitle = (CheckedTextView) findViewById(R.id.ctv_title);
        mTvLabel = (TextView) findViewById(R.id.tv_label);

        mCtvIcon.setEnabled(false);
        mCtvTitle.setEnabled(false);
        setIvIcon();
        setTvTitle();
        setTvLabel();
    }

    private void setIvIcon(){
        mIvIconBkg=mTypedArray.getResourceId(R.styleable.radio_item_ic_bkg,-1);
        mIcWidth=mTypedArray.getDimensionPixelSize(R.styleable.radio_item_ic_width, LayoutParams.WRAP_CONTENT);
        mIcHeight=mTypedArray.getDimensionPixelSize(R.styleable.radio_item_ic_height, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) mCtvIcon.getLayoutParams();
        layoutParams.width= (int) mIcWidth;
        layoutParams.height= (int) mIcHeight;
        mCtvIcon.setLayoutParams(layoutParams);
        if(-1!=mIvIconBkg){
            mCtvIcon.setBackgroundResource(mIvIconBkg);
        }else{
            mCtvIcon.setVisibility(GONE);
        }

    }

    private void setTvTitle(){
        mTitleSize=mTypedArray.getFloat(R.styleable.radio_item_title_size, 12);
        mTitleColorId=mTypedArray.getResourceId(R.styleable.radio_item_title_color, -1);
        mTitle=mTypedArray.getString(R.styleable.radio_item_title_text);
        mCtvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTitleSize);
        mCtvTitle.setText(mTitle);
        if(-1!=mTitleColorId){
            mCtvTitle.setTextColor(getResources().getColorStateList(mTitleColorId));
        }
    }


    private void setTvLabel(){
        mLableVisiable=mTypedArray.getBoolean(R.styleable.radio_item_label_visiable,false);
        mLabelBkgId=mTypedArray.getResourceId(R.styleable.radio_item_label_bkg, -1);
        mLabelColorId=mTypedArray.getResourceId(R.styleable.radio_item_label_color, -1);
        mLabelSize=mTypedArray.getFloat(R.styleable.radio_item_label_size, 10);
        mLabelTopMargin=mTypedArray.getDimensionPixelSize(R.styleable.radio_item_label_topmargin,0);
        mLabelLeftMargin =mTypedArray.getDimensionPixelSize(R.styleable.radio_item_label_leftmargin,0);
        mLabelHeight=mTypedArray.getDimensionPixelSize(R.styleable.radio_item_label_height,getContext().getResources().getDimensionPixelSize(R.dimen.rg_item_def_height));
        mTvLabel.setTextSize(TypedValue.COMPLEX_UNIT_SP,mLabelSize);
        RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) mTvLabel.getLayoutParams();
        layoutParams.topMargin=  mLabelTopMargin;
        layoutParams.leftMargin= mLabelLeftMargin;
        if(mLabelHeight>0){
            layoutParams.height= (int) mLabelHeight;
            mTvLabel.setMinWidth((int) mLabelHeight);
        }
        mTvLabel.setLayoutParams(layoutParams);
        if(-1!=mLabelBkgId){
            mTvLabel.setBackgroundResource(mLabelBkgId);
        }
        if(-1!=mLabelColorId){
            mTvLabel.setTextColor(getResources().getColor(mLabelColorId));
        }
        if(mLableVisiable){
            mTvLabel.setVisibility(ViewFlipper.VISIBLE);
        }else{
            mTvLabel.setVisibility(ViewFlipper.INVISIBLE);
        }
    }


    public void setchecked(boolean check){
        if(check){
            mCtvIcon.setChecked(true);
            mCtvTitle.setChecked(true);
        }else{
            mCtvIcon.setChecked(false);
            mCtvTitle.setChecked(false);
        }
    }


    public CheckedTextView getCrvIcon() {
        return mCtvIcon;
    }

    public CheckedTextView getCtvTitle() {
        return mCtvTitle;
    }

    public TextView getTvLabel() {
        return mTvLabel;
    }
}
